// pages/index/index.js
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userID: "",
    userName: "",
    foodList: [],
    foodNowType: null,
    userImage: "/image/index/avatar.png",
    foodType: [
      { text: "不限类" },
      { text: "主食类" },
      { text: "肉蛋类" },
      { text: "海鲜类" },
      { text: "蔬菜类" },
      { text: "菌菇类" },
      { text: "豆奶类" },
    ]
  },

  jumpLogin: function() {
    if(!this.data.userName) {
      wx.navigateTo({
        url: '../login/login',
      })
    }
  },

  jumpFood: function(e) {
    if(!this.data.userName) {
      wx.navigateTo({
        url: '../login/login',
      })
    } else {
      const foodItem = e.currentTarget.dataset.food;
      // console.log(foodItem)
      wx.navigateTo({
        url: '../food/food?food=' + JSON.stringify(foodItem)
      });
    }
  },

  jumpList: function() {
    wx.switchTab({
      url: '../list/list',
    })
  },

  jumpOrder: function() {
    wx.switchTab({
      url: '../order/order',
    })
  },

  jumpRecommend: function() {
    if(!this.data.userName) {
      wx.navigateTo({
        url: '../login/login',
      })
    } else {
      wx.navigateTo({
        url: '../recommend/recommend',
      })    
    }
  },

  getNowType: function(e) {
    const nowType = e.currentTarget.dataset.type;
    this.data.foodNowType = nowType;
    this.getFoodList();
  },

  getFoodList: function(e) {
    const foodData = {
      foodName: null,
      foodType: this.data.foodNowType,
    };
    // console.log(foodData)
    wx.request({
      url: 'http://localhost:9090/food/queryFood',
      method: 'POST',
      data: foodData,
      header: {
        'Content-Type': 'application/json'
      },
      success:(res) => {
        // console.log(res.data.data)
        if(res.data.code == "200") {
          this.setData({
            foodList: res.data.data
          })
        }
        // console.log(this.data.foodList)
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      userID: app.globalData.userID,
      userName: app.globalData.userName
    });
    this.getFoodList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      userID: app.globalData.userID,
      userName: app.globalData.userName
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})